<?php
namespace app\admin\controller;

use think\facade\View;
use app\common\business\Category as CategoryBus;

class Category extends AdminBase
{
    /**
     * 商品分类列表
     * @return string
     * @throws \Exception
     */
    public function index()
    {
        $pid = input('param.pid',0, 'intval');
        $data = [
            'pid' => $pid,
        ];

        try {
            $categorys = (new CategoryBus())->getLists($data, 5);
        } catch (\Exception $e) {
            $categorys = [
                'total' => 0,
                'per_page' => 5,
                'current_page' => 1,
                'last_age' => 0,
                'data' => [],
            ];
        }


        return View::fetch("",[
            'categorys' => $categorys,
            'pid' => $pid
        ]);

    }

    /**
     * 添加分类
     * @return string
     * @throws \Exception
     */
    public function add()
    {

        try {
            $categorys = (new CategoryBus())->getNormalCategorys();
        } catch (\Exception $e) {
            $categorys = [];
        }

        return View::fetch("",[
            'categorys' => json_encode($categorys),
        ]);
    }

    public function update()
    {
        $pid = input('param.pid',0,'intval');
        $id = input('param.id',0,'intval');

        $data = [
            'pid' => $pid,
            'id' => $id,
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('category_update')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        $result = (new CategoryBus())->getById($id);

        $parentResult = (new CategoryBus())->getById($pid);
        if (empty($parentResult)) {
            $parentResult['name'] = '顶级分类';
        }


        $result['pname'] = $parentResult['name'];

        return View::fetch("", [
            'result' => $result,
        ]);

    }

    /**
     * 保存修改分类
     * @return \think\response\Json
     */
    public function updatesave()
    {
        if (!$this->request->isAjax()) {
            return show(config('status.error'), '请求异常');
        }

        $pid = input('param.pid', 0, 'intval');
        $id = input('param.id', 0, 'intval');
        $name = input('param.name','','trim');

        $data = [
            'pid' => $pid,
            'name' => $name,
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('category_add')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        $updateData = [
            'id' => $id,
            'name' => $name,
        ];

        try {
            $result = (new CategoryBus())->updateCategoryName($updateData);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($result) {
            return show(config('status.success'),'更新分类成功');
        }

        return show(config('status.error'),'更新分类失败');
    }

    /**
     * 保存分类
     * @return \think\response\Json
     */
    public function save()
    {
        if (!$this->request->isAjax()) {
            return show(config('status.error'), '请求异常');
        }

        $pid = input('param.pid', 0, 'intval');
        $name = input('param.name','','trim');

        $data = [
            'pid' => $pid,
            'name' => $name,
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('category_add')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        try {
            $result = (new CategoryBus())->add($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($result) {
            return show(config('status.success'),'添加分类成功');
        }

        return show(config('status.error'),'添加分类失败');
    }

    /**
     * 分类排序
     * @return \think\response\Json
     */
    public function listorder()
    {
        $id = input('param.id', 0, 'intval');
        $listorder = input('param.listorder', 0, 'intval');

        $data = [
            'id' => $id,
            'listorder' => $listorder,
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('category_listorder')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        try {
            $res = (new CategoryBus())->listorder($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return show(config('status.success'),'排序成功');
        }

        return show(config('status.error'),'排序失败');
    }

    /**
     * 修改状态
     * @return \think\response\Json
     */
    public function status()
    {
        $id = input('param.id', 0, 'intval');
        $status = input('param.status', 0, 'intval');

        $data = [
            'id' => $id,
            'status' => $status,
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('category_status')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        try {
            $res = (new CategoryBus())->status($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($res) {
            return show(config('status.success'),'状态更新成功');
        }

        return show(config('status.error'),'状态更新失败');
    }

    public function dialog()
    {
        $categorys = (new CategoryBus())->getNormalByPid();

        return view('',[
            'categorys' => json_encode($categorys),
        ]);
    }

    public function getByPid()
    {
        $pid = input('param.pid',0,'intval');
        $categorys = (new CategoryBus())->getNormalByPid($pid);

        return show(config('status.success'),'OK',$categorys);

    }
}
